package com.study.queue;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/6/7 0:45
 */
public class Message implements Delayed
{
    private int id;
    private String body; // 消息内容
    private long executeTime;// 延迟时长，这个是必须的属性因为要按照这个判断延时时长。

    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    public String getBody()
    {
        return body;
    }

    public void setBody(String body)
    {
        this.body = body;
    }

    public long getExecuteTime()
    {
        return executeTime;
    }

    public void setExecuteTime(long executeTime)
    {
        this.executeTime = executeTime;
    }

    public Message(int id, String body, long executeTime)
    {
        this.id = id;
        this.body = body;
        this.executeTime = executeTime;
    }


    @Override
    public int compareTo(Delayed o)
    {
        Message msg = (Message) o;
        return Integer.compare(this.id, msg.id);
    }

    @Override
    public long getDelay(TimeUnit unit)
    {
        return unit.convert(this.executeTime - System.nanoTime(), TimeUnit.NANOSECONDS);
    }
}
